﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;

namespace MOJsdk
{
    public class MojToolButton : Button, IComparable, LogicalClass, IEquatable<MojToolButton>
    {
        public string Letter { get; set; }
        public string Image { get; set; }
        public string ToolTipText { get; set; }
        public string UniqueID { get; set; }

        public MojToolButton(string letter, string uniqueID, string tooltiptext, string imagePath)
        {
            _showTypes = new List<LogicStatment>();
            Letter = letter;
            UniqueID = uniqueID;

            if (!Letter.IsNullorEmpty())
                this.Content = Letter;

            if (tooltiptext != null)
                ToolTipText = tooltiptext;
            else
                ToolTipText = "";
            ToolTip = ToolTipText;

            if (System.IO.File.Exists(imagePath))
            {
                Image = imagePath;
            }
            else
                Image = null;
        }

        private List<LogicStatment> _showTypes;
        public List<LogicStatment> ShowConditions
        { get { return _showTypes; } }

        #region AddCondition
        public void AddLogicStatment(LogicStatment statment)
        {
            _showTypes.Add(statment);
            CheckVisibility();
        }
        #endregion

        #region Check
        public void SetVisibility(Visibility VisibilityType)
        {
            this.Visibility = VisibilityType;
        }
        public void CheckVisibility()
        {
            foreach (var v in _showTypes)
                if (TestStatment(v))
                {
                    SetVisibility(System.Windows.Visibility.Visible);
                    return;
                }

            SetVisibility(System.Windows.Visibility.Hidden);
        }
        #endregion

        public bool TestStatment(LogicStatment statment)
        {
            string method = statment.BuildClass("MojToolButton THIS = (MojToolButton)elem\n");
            return ScriptInterop.RunLogicMethod(method, this);
        }

        public int CompareTo(object obj)
        {
            MojToolButton btn = obj as MojToolButton;
            return UniqueID.CompareTo(btn.UniqueID);
        }
        public bool Equals(MojToolButton btn)
        {
            return UniqueID == btn.UniqueID;
        }
    }
    public class MojToolButtonCollection
    {
        private ObservableCollection<MojToolButton> _buttons;

        public MojToolButtonCollection()
        {
            _buttons = new ObservableCollection<MojToolButton>();
        }

        public void Add(MojToolButton ToolBtn)
        {
            if (_buttons.Contains(ToolBtn))
                return;

            ToolBtn.Width = 20;
            ToolBtn.Height = 20;
            ToolBtn.ApplyStyle(ToolBtn.UniqueID);

            _buttons.Add(ToolBtn);
            Update();
        }
        public void Remove(MojToolButton ToolBtn)
        {
            if (_buttons.Contains(ToolBtn))
            {
                _buttons.Remove(ToolBtn);
                Update();
            }
        }

        public void Update()
        {
            UpdateVisibility();
            Add2StackPanel();
        }
        private void UpdateVisibility()
        {
            foreach (var b in _buttons)
                b.CheckVisibility();

            Add2StackPanel();
        }
        private void Add2StackPanel()
        {
            MojHost.GetHost.TopToolBar.Children.Clear();
            foreach (var v in _buttons)
                if(v.Visibility == Visibility.Visible)
                    MojHost.GetHost.TopToolBar.Children.Add(v);
        }
    }
    public class MojToolButtonShowCondition
    {
        public static LogicStatment ShowAllways
        {
            get 
            {
                return new LogicStatment()
                {
                    Code = "true",
                    Librarys = "",
                };
            }
        }
        public static LogicStatment IPanel_MovieBrowser
        {
            get
            {
                return new LogicStatment()
                    {
                        Code = "MojStatusVarsEnum.DisplayPanel.Value().PanelType == \"MovieBrowser\"",
                        Librarys = "",
                    };
            }
        }
        public static LogicStatment IPanel_ManageMovies
        {
            get
            {
                return new LogicStatment()
                {
                    Code = "MojStatusVarsEnum.DisplayPanel.Value().PanelType == \"ManageMovies\"",
                    Librarys = "",
                };
            }
        }
        public static LogicStatment IsMovieSelected
        {
            get
            {
                return new LogicStatment()
                {
                    Code = "MojStatusVarsEnum.IsMovieSelected.Value()",
                    Librarys = "",
                };
            }
        }
        public static LogicStatment IsFileSelected
        {
            get
            {
                return new LogicStatment()
                {
                    Code = "MojStatusVarsEnum.IsFileSelected.Value()",
                    Librarys = "",
                };
            }
        }
    }
}



























